package com.sd.repay.service.impl;

import com.sd.repay.common.Constants;
import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.MerchantUser;
import com.sd.repay.service.CardManageService;
import com.sd.repay.service.MerchantService;
import com.sd.repay.utils.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 统一卡片Service接口实现类
 * @date 2017年10月25日
 */
@Service
public class CardManageServiceImpl implements CardManageService {

    private static final Logger log = LoggerFactory.getLogger(CardManageServiceImpl.class);

    @Autowired
    private BaseDao baseDao;
    @Autowired
    private MerchantService merchantService;

    /**
     * 添加统一卡片信息
     *
     * @param cardInfo
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int addCardInfo(Map<String, String> cardInfo) throws Exception {

        String merchantNo = cardInfo.get("merchantNo");
        String accountName = cardInfo.get("accountName");
        String idCardNo = cardInfo.get("idCardNo");

        String sql = " INSERT INTO t_card_manage(card_no,id_card_no,mobile_no,account_no,account_name,card_type,is_settle_card,settle_bank_no,bank_name,bank_no,province,city " +
                " ,statement_date,repayment_date,create_time) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        List<Object> params = new ArrayList<>();
        params.add(cardInfo.get("cardNo"));
        params.add(idCardNo);
        params.add(cardInfo.get("mobileNo"));
        params.add(cardInfo.get("accountNo"));
        params.add(accountName);
        params.add(cardInfo.get("cardType"));
        params.add(cardInfo.get("isSettleCard"));
        params.add(cardInfo.get("settleBankNo"));
        params.add(cardInfo.get("bankName"));
        params.add(cardInfo.get("bankNo"));
        params.add(cardInfo.get("province"));
        params.add(cardInfo.get("city"));
        params.add(cardInfo.get("statementDate"));
        params.add(cardInfo.get("repaymentDate"));
        params.add(new Date());

        int count = baseDao.execute(sql, params.toArray());
        if (count > 0) {
            //是否修改商户实名认证信息
            String isUpdateMerAuth = cardInfo.get("isUpdateMerAuth");
            if ("true".equals(isUpdateMerAuth)) {
                String address = cardInfo.get("address");
                String filePath = cardInfo.get("filePath");
                count = merchantService.updateMerAuthInfo(merchantNo, accountName, idCardNo, address, filePath);
                log.info("商户号[{}]添加卡片[{}]修改实名认证状态影响的行数[{}]", new Object[]{merchantNo, cardInfo.get("accountNo"), count});
                if (count < 1) {
                    throw new RuntimeException("商户添加结算卡修改实名认证信息失败");
                }
                //更新shiro Session中的商户信息
                MerchantUser currUser = WebUtil.getCurrMerchantUser();
                currUser.setIsAuth(Constants.MER_AUTH_STATUS_PASS);
                currUser.setRealName(accountName);
                currUser.setIdCardNo(idCardNo);
                currUser.setAddress(address);
            }
        }
        return count;
    }

    /**
     * 根据账户号获取统一卡片对象
     *
     * @param accountNo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getCardManageByAccountNo(String accountNo) throws Exception {
        String sql = " SELECT * FROM t_card_manage WHERE account_no = ? ";
        return baseDao.findFirst(sql, accountNo);
    }

    /**
     * 根据卡片编号获取统一卡片管理对象
     *
     * @param cardNo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getCardManageByCardNo(String cardNo) throws Exception {
        String sql = " SELECT * FROM t_card_manage WHERE card_no = ? ";
        return baseDao.findFirst(sql, cardNo);
    }

    /**
     * 根据身份证获取所有卡片信息
     *
     * @param idCradNo
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> getCardInfoByIdCard(String idCradNo) throws Exception {
        String sql = " SELECT * FROM t_card_manage WHERE id_card_no = ? ";
        return baseDao.findList(sql, idCradNo);
    }

    /**
     * @param idCrad
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getSettleCardInfoByIdCard(String idCrad) throws Exception {

        String sql = " SELECT * FROM t_card_manage WHERE id_card_no = ? AND card_type = ? AND is_settle_card = ? ";
        return baseDao.findFirst(sql, new Object[]{idCrad, Constants.CARD_TYPE_DEBIT, "1"});
    }

    /**
     * 修改结算卡状态
     *
     * @param cardNo
     * @param isSettleStatus
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public int updateSettleStatus(String cardNo, String isSettleStatus) throws Exception {
        String sql = " UPDATE t_card_manage SET is_settle_card = ? WHERE card_no = ? ";
        return baseDao.execute(sql, new Object[]{isSettleStatus, cardNo});
    }

    /**
     * 删除卡片信息
     *
     * @param cardNo
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public int deleteCard(String cardNo) throws Exception {

        String sql = " DELETE FROM t_card_manage WHERE card_no = ? ";
        return baseDao.execute(sql, cardNo);
    }

    @Override
    @Transactional
    public int updateCardInfo(String accountNo, String statementDate, String repaymentDate, String province, String city) throws Exception {

        String sql = "UPDATE t_card_manage SET province = ?, city = ?, statement_date = ?, repayment_date = ? WHERE account_no = ? ";
        return baseDao.execute(sql, new Object[]{province, city, statementDate, repaymentDate, accountNo});
    }

    /**
     * 获取所有通道不支持卡列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> getExcludeCard() throws Exception {
        String sql = "SELECT * FROM t_exclude_card";
        return baseDao.findList(sql);
    }


    /**
     * 获取指定通道不支持卡列表
     *
     * @param channelCode
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> getExcludeCardByChannel(String channelCode) throws Exception {
        String sql = " SELECT * FROM t_exclude_card WHERE channel_code = ? ";
        return baseDao.findList(sql, channelCode);
    }

    /**
     * 卡bin表获取卡信息
     *
     * @param accountNo
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getPosCardInfoByAccountNo(String accountNo) throws Exception {

        String sql = " SELECT * FROM t_pos_card_bin WHERE card_length = length(?) AND verify_code = LEFT(?, verify_length) ";
        return baseDao.findFirst(sql, new Object[]{accountNo, accountNo});
    }


}
